from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup

def getTitle(bs4Obj):
    title = bs4Obj.h1
    return title

try:
    html = urlopen('http://php.cppedu.net')

    try:
        bs4Obj = BeautifulSoup(html.read(), "html.parser")
        title = getTitle(bs4Obj)
        if title is None:
            print('抓取的标签不存在！')
        else:
            print(title)

    except AttributeError as e:
        print('抓取的标签不存在！'+e)

except HTTPError as e:
    print('url访问失败:'+e)




